package com.itheima.web.controller;

import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    // 分页查询
    @GetMapping("/manage/users")
    public ResponseEntity findUserInfoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return userManager.findUserInfoByPage(pageNum, pageSize);
    }

    // 用户详情
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId) {
        // 调用manager
        return userManager.findUserInfoById(userId);
    }

    // 动态列表
    @GetMapping("/manage/messages")
    public ResponseEntity findMovementVoByPage(
            Integer state,
            @RequestParam(value = "uid", required = false) Long userId,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return userManager.findMovementVoByPage(state, userId, pageNum, pageSize);
    }

    // 动态详情
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity findMovementVoById(@PathVariable String publishId) {
        // 调用manager
        return userManager.findMovementVoById(publishId);
    }


    // 动态评论列表
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findCommentVoByPage(
            @RequestParam("messageID") String publishId,
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        // 调用manager
        return userManager.findCommentVoByPage(publishId,pageNum,pageSize);
    }
    //查询视频列表
    @GetMapping("/manage/videos")
    public ResponseEntity findVideoByPage(@RequestParam(value = "uid")Long userId,
                                          @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){
        //调用manager
        return userManager.findVideoByPage(userId,pageNum,pageSize);
    }

    //冻结用户
    @PostMapping("/manage/users/freeze")
    public void freezeByUser(@RequestBody Map<String, String> param){
        //接收参数
        Long userId = Long.parseLong(param.get("userId"));//用户id
        int freezingTime = Integer.parseInt(param.get("freezingTime"));//冻结时间
        int freezingRange = Integer.parseInt(param.get("freezingRange"));//冻结范围
        String reasonsForFreezing = param.get("reasonsForFreezing");//冻结原因
        String frozenRemarks = param.get("frozenRemarks");//冻结备注


        userManager.freezeByUser(userId, freezingTime,freezingRange,reasonsForFreezing,frozenRemarks);
    }

    //解冻用户
    @PostMapping("/manage/users/unfreeze")
    public void unfreezeByUser(@RequestBody Map<String, String> param){
        Long userId = Long.parseLong(param.get("userId"));//用户id
        String reasonsForThawing = param.get("frozenRemarks");//解冻原因

        userManager.unfreezeByUser(userId,reasonsForThawing);
    }
}
